package com.Data;


import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import com.Managers.UserManagerClass;
import com.SoapClasses.SoapPost;
import com.SoapClasses.SoapUtil;
import com.shareup.R;
import com.utils.AlertDialogHelper;


public class Post extends AsyncTask<String, String, Post[]> {

    public Context getContext() {

        return this.context;
    }

    public void setContext(Context context) {

        this.context = context;
    }

    public static final int ACTION_DELETE = 300;

    public static final int ACTION_GET_LIKED_PICTURE = 500;

    public static final int ACTION_UPLOAD = 200;

    public static final int ACTION_UPLOAD_COVER = 400;

    public static final int ACTION_UPLOAD_PROFILE = 100;

    public static final int BLURCOMMENTS = 50;

    public static final int BLURCOVER = 10;

    private onDeletePictureCompleted callbackDeletePicture;

    private OnGetPicturesCompleted callbackGetPictures;

    private onUploadPictureCompleted callbackUploadPicture;

    private String Comment;

    private Context context;

    private int fromPosition;

    private GeoTag GeoTag;

    private int Height;

    private int ID;

    private boolean isCover;

    private int Length;

    private int MODE;

    private int myID;

    private String PathFull;

    private String PathThumb;

    private int Permission;

    private byte[] PicBytes;

    private PictureInfo PictureCounters;

    private int Private;

    private AlertDialog progress;

    private String PublicationDate;

    private String tag;

    private String Tags;

    private int Type;

    private String UploadedFrom;

    private User User;

    private int userID;

    private int Width;

    private Albums Album;

    public Albums getAlbum() {

        return this.Album;
    }

    public void setAlbum(Albums album) {

        this.Album = album;
    }

    public interface onDeletePictureCompleted {

        void Completed(String result);
    }

    public interface OnGetPicturesCompleted {

        void Completed(Post[] result);

    }

    public interface onUploadPictureCompleted {

        void Completed(String result);
    }

    public Post() {

    }

    public Post(int MODE, Context context, byte[] bytesPicture) {

        this.MODE = MODE;
        this.context = context;
        this.PicBytes = bytesPicture;
    }

    public Post(int MODE) {

        this.MODE = MODE;
    }

    public Post(int MODE, int position) {

        this.MODE = MODE;
        this.fromPosition = position;
    }

    public Post(int fromPosition, int userID, Context context,
                int mODE, String tag, OnGetPicturesCompleted callback) {

        super();
        this.fromPosition = fromPosition;
        this.myID = UserManagerClass.UserLogged.getID();
        this.userID = userID;
        this.context = context;
        this.MODE = mODE;
        this.tag = tag;
        this.callbackGetPictures = callback;
    }

    public onDeletePictureCompleted getCallbackDeletePicture() {

        return callbackDeletePicture;
    }

    public OnGetPicturesCompleted getCallbackGetPictures() {

        return callbackGetPictures;
    }

    public onUploadPictureCompleted getCallbackUploadPicture() {

        return callbackUploadPicture;
    }

    public String getComment() {

        return Comment;
    }

    public GeoTag getGeoTag() {

        return GeoTag;
    }

    public int getHeight() {

        return Height;
    }

    public int getID() {

        return ID;
    }

    public int getLength() {

        return Length;
    }

    public String getPathFull() {

        return PathFull;
    }

    public String getPathThumb() {

        return PathThumb;
    }

    public int getPermission() {

        return this.Permission;
    }

    public PictureInfo getPictureCounters() {

        return PictureCounters;
    }

    public int getPrivate() {

        return Private;
    }

    public String getPublicationDate() {

        return PublicationDate;
    }

    public String getTags() {

        return Tags;
    }

    public int getType() {

        return Type;
    }

    public String getUploadedFrom() {

        return UploadedFrom;
    }

    public User getUser() {

        return User;
    }

    public int getWidth() {

        return Width;
    }

    public boolean isCover() {

        return this.isCover;
    }

    public void setCallbackDeletePicture(onDeletePictureCompleted callbackDeletePicture) {

        this.callbackDeletePicture = callbackDeletePicture;
    }

    public void setCallbackGetPictures(OnGetPicturesCompleted callbackGetPictures) {

        this.callbackGetPictures = callbackGetPictures;
    }

    public void setCallbackUploadPicture(onUploadPictureCompleted callbackUploadPicture) {

        this.callbackUploadPicture = callbackUploadPicture;
    }

    public void setComment(String comment) {

        Comment = comment;
    }

    public void setCover(boolean isCover) {

        this.isCover = isCover;
    }

    public void setGeoTag(GeoTag geoTag) {

        GeoTag = geoTag;
    }

    public void setHeight(int height) {

        Height = height;
    }

    public void setID(int iD) {

        ID = iD;
    }

    public void setLength(int length) {

        Length = length;
    }

    public void setPathFull(String pathFull) {

        PathFull = pathFull;
    }

    public void setPathThumb(String pathThumb) {

        PathThumb = pathThumb;
    }

    public void setPermission(int permission) {

        this.Permission = permission;
    }

    public void setPictureCounters(PictureInfo pictureCounters) {

        PictureCounters = pictureCounters;
    }

    public void setPrivate(int private1) {

        Private = private1;
    }

    public void setPublicationDate(String publicationDate) {

        PublicationDate = publicationDate;
    }

    public void setTags(String tags) {

        Tags = tags;
    }

    public void setType(int type) {

        Type = type;
    }

    // //////////////////////////////////////////////////////

    public void setUploadedFrom(String uploadedFrom) {

        UploadedFrom = uploadedFrom;
    }

    public void setUser(User user) {

        User = user;
    }

    public void setWidth(int width) {

        Width = width;
    }

    @Override
    protected Post[] doInBackground(String... data) {

        SoapPost sp = new SoapPost();
        String res;
        Post[] ps;
        Post pc;
        //
        switch (MODE) {
            case SoapUtil.MODE_EXPLORE:
                return sp.getExplorePictures(fromPosition, userID, myID, context);
            case SoapUtil.MODE_NEWS:
                return sp.getNewsPictures(fromPosition, userID, myID, context);
            case SoapUtil.MODE_MINE:
                return sp.getMyPictures(fromPosition, userID, myID, context);
            case SoapUtil.MODE_FAVS:
                return sp.getFavsPictures(fromPosition, userID, myID, context);
            case SoapUtil.MODE_FOLLOWERS:
                return sp.getFriendPictures(userID, myID, context);
            case SoapUtil.MODE_TAGS:
                return sp.getTagsPictures(fromPosition, userID, myID, tag, context);
            case SoapUtil.MODE_ALBUM:
                return sp.getAlbumPictures(fromPosition, userID, myID, context);
            case SoapUtil.MODE_GET_PUBLICATION_INFO:
                return sp.getPublicationInfo(this.ID);
                // HANDLING ACTIONS
            case ACTION_UPLOAD:
                res = sp.uploadPicture(data, this.getTags(), SoapPost.UPLOAD_PICTURE, this.PicBytes);
                ps = new Post[1];
                pc = new Post();
                pc.setComment(res);
                ps[0] = pc;
                return ps;
            case ACTION_UPLOAD_PROFILE:
                res = sp.uploadPicture(data, null, SoapPost.UPLOAD_PROFILE, this.PicBytes);
                ps = new Post[1];
                pc = new Post();
                pc.setComment(res);
                ps[0] = pc;
                return ps;
            case ACTION_UPLOAD_COVER:
                res = sp.uploadPicture(data, null, SoapPost.UPLOAD_COVER, this.PicBytes);
                ps = new Post[1];
                pc = new Post();
                pc.setComment(res);
                ps[0] = pc;
                return ps;
            case ACTION_DELETE:
                res = sp.deletePicture(this.User.getID(), this.ID);
                ps = new Post[1];
                pc = new Post();
                pc.setComment(res);
                ps[0] = pc;
                return ps;
            case ACTION_GET_LIKED_PICTURE:
                res = sp.getLikedPicture();
                ps = new Post[1];
                pc = new Post();
                pc.setPathFull(res);
                ps[0] = pc;
                return ps;
            default:
                return null;
        }
    }

    @Override
    protected void onPostExecute(Post[] result) {

        if (MODE == ACTION_UPLOAD || MODE == ACTION_UPLOAD_PROFILE
                    || MODE == ACTION_UPLOAD_COVER
                    || MODE == ACTION_DELETE)
            progress.dismiss();

        if (callbackGetPictures != null)
            callbackGetPictures.Completed(result);

        if (callbackUploadPicture != null)
            callbackUploadPicture.Completed(result[0].getComment());

        if (callbackDeletePicture != null)
            callbackDeletePicture.Completed(result[0].getComment());

    }

    @Override
    protected void onPreExecute() {

        String mess = "";

        if (MODE == ACTION_UPLOAD || MODE == ACTION_UPLOAD_PROFILE) {
            if (context != null)
                mess = context.getResources().getString(R.string.dialog_uploading_picture);

        }
        if (MODE == ACTION_UPLOAD_COVER)
            if (context != null)
                mess = context.getResources().getString(R.string.dialog_saving);

        if (MODE == ACTION_DELETE)
            if (context != null)
                mess = context.getResources().getString(R.string.dialog_deleting);

        if (!mess.equals("")) {
            if (context != null) {
                progress = AlertDialogHelper.setProgressDialog(progress, mess, context);
                progress.show();
            }

        }

    }

    public Bitmap getThumb() {

        return thumb;
    }

    public void setThumb(Bitmap thumb) {

        this.thumb = thumb;
    }

    /**
     * @param bitmap
     */
    private Bitmap thumb;

}
